package com.heima.gateway.filter;

import lombok.Data;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.OrderedGatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;
//该类的名称一定要以GatewayFilterFactory为后缀！
//Print对应配置文件中参数名称
@Component
public class PrintGatewayFilterFactory extends AbstractGatewayFilterFactory<PrintGatewayFilterFactory.Config> {

    @Override
    public GatewayFilter apply(PrintGatewayFilterFactory.Config config) {
        //        不能设置过滤器链权重
//        return new GatewayFilter(){
//            @Override
//            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//                System.out.println("print any filter running");
//                return chain.filter(exchange);
//            }
//        };

        return new OrderedGatewayFilter(new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                System.out.println(config.getA());
                System.out.println(config.getB());
                System.out.println(config.getC());
                System.out.println("print any filter running");
                return chain.filter(exchange);
            }
        },1);
    }

    @Data
    public static class Config{
        private String a;
        private String b;
        private String c;
    }

    //     将变量名称依次返回，对应配置文件中参数顺序
    public List<String> shortcutFieldOrder(){
        return List.of("a","b","c");
    }
    //    将Config字节码传递给父类，父类负责帮我们读取yaml文件
    public PrintGatewayFilterFactory(){
        super(Config.class);
    }
}
